package patrol.mobile.com.mobilepatrol.manager;

import java.util.List;

import io.realm.Realm;
import io.realm.RealmList;
import io.realm.RealmResults;
import io.realm.Sort;
import patrol.mobile.com.mobilepatrol.PatrolApp;
import patrol.mobile.com.mobilepatrol.bean.Handong;
import patrol.mobile.com.mobilepatrol.bean.HddqjcBjshb;
import patrol.mobile.com.mobilepatrol.bean.Hddqjcb;
import patrol.mobile.com.mobilepatrol.bean.HdjcxjcShqkb;
import patrol.mobile.com.mobilepatrol.bean.Hdjcxjcb;

import static patrol.mobile.com.mobilepatrol.PatrolApp.realm;

/**
 * Created by apple on 17/9/28.
 * 获取定期列表
 * 定期缺损列表
 * 经常列表
 * 经常缺损列表
 *
 */

public class HManager {
    public static RealmResults<Handong> getHList() {
        return realm.where(Handong.class).findAll();
    }
    public static Handong getHMes(String hdcode) {
        List<Handong> hList = getHList();
        for (int i = 0; i < hList.size(); i++) {
            String curCode = hList.get(i).hdcode;
            if (hdcode.equals(curCode)) {
                return hList.get(i);
            }
        }
        return null;
    }

    /**
     * 获取涵洞定期检查列表，逆序
     */
    public static List<Hddqjcb> getHDDQList(String hdcode) {
        RealmResults<Hddqjcb> results = PatrolApp.realm.where(Hddqjcb.class).equalTo("hdcode",hdcode).findAll();
        List<Hddqjcb> mList = results.sort("jcsj", Sort.DESCENDING);
            return setDQIndex(mList);
    }
    /**
     * 获取涵洞经常检查列表，逆序
     */
    public static List<Hdjcxjcb> getHDJCList(String hdcode) {
        RealmResults<Hdjcxjcb> results = PatrolApp.realm.where(Hdjcxjcb.class).equalTo("hdcode",hdcode).findAll();
        List<Hdjcxjcb> mList = results.sort("jcsj", Sort.DESCENDING);
            return setJCIndex(mList);
    }

    /**
     * 删除一个涵洞定期检查对象表
     * 涵洞定期检查表中有一个涵洞定期缺损列表
     * @param hddqjcid
     */
    public static void delCulvertDQ(final String hddqjcid) {
        realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                RealmResults<Hddqjcb> br = realm.where(Hddqjcb.class).equalTo("id",hddqjcid).findAll();
                Hddqjcb alter = br.get(0);
                RealmResults<HddqjcBjshb> qs = realm.where(HddqjcBjshb.class).equalTo("id",hddqjcid).findAll();
                qs.deleteAllFromRealm();
                alter.deleteFromRealm();
            }
        });
    }
    public static void delCulvertJC(final String hdjcxjcid) {
        realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                RealmResults<Hdjcxjcb> br = realm.where(Hdjcxjcb.class).equalTo("id",hdjcxjcid).findAll();
                RealmResults<HdjcxjcShqkb> qs = realm.where(HdjcxjcShqkb.class).equalTo("id",hdjcxjcid).findAll();
                qs.deleteAllFromRealm();
                br.deleteAllFromRealm();
            }
        });
    }
    public static String isExit(String code) {
        List<Handong> hList = getHList();
        for (int i = 0; i < hList.size(); i++) {
            String curCode = hList.get(i).hdcode;
            if (code.equals(curCode)) {
                return hList.get(i).hdcode;
            }
        }
        return null;
    }
    //获取涵洞经常检查obj
    public static Hdjcxjcb getCulvertJC(String hdcode, String hdjcxjcid) {
        RealmList<Hdjcxjcb> hdjcxjcbs = getHMes(hdcode).jcxjc;
        for (int i = 0; i < hdjcxjcbs.size(); i++) {
            Hdjcxjcb item = hdjcxjcbs.get(i);
            if (hdjcxjcid.equals(item.id)) {
                return item;
            }
        }
        return null;
    }
    //获取涵洞定期检查obj
    public static Hddqjcb getCulvertDQ(String hdcode, String hddqjcid) {
        RealmList<Hddqjcb> hddqjcbs = getHMes(hdcode).dqjc;
        for (int i = 0; i < hddqjcbs.size(); i++) {
            Hddqjcb item = hddqjcbs.get(i);
            if (hddqjcid.equals(item.id)) {
                return item;
            }
        }
        return null;
    }
    //获取一个涵洞定期检查对象缺损列表
    public static List<HddqjcBjshb> getHDDQQSList(final String hddqjcid) {
                RealmResults<HddqjcBjshb> mList = realm.where(HddqjcBjshb.class).equalTo("hddqjcid",hddqjcid).findAll();
            return setDQQSIndex(mList);
    }
    //获取一个涵洞经常检查对象缺损列表
    public static List<HdjcxjcShqkb> getHDJCQSList(final String hdjcxjcid) {
        RealmResults<HdjcxjcShqkb> mList = realm.where(HdjcxjcShqkb.class).equalTo("hdjcxjcid",hdjcxjcid).findAll();
            return setJCQSIndex(mList);
    }
    public static void alterCulvertJC(final Hdjcxjcb b) {
        realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                RealmResults<Hdjcxjcb> br = realm.where(Hdjcxjcb.class).equalTo("id",b.id).findAll();
                Hdjcxjcb alter = br.get(0);
                alter.jlr = b.jlr;
                alter.fzr = b.fzr;
            }
        });
    }
    public static void alterCulvertDQ(final Hddqjcb b) {
        realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                RealmResults<Hddqjcb> br = realm.where(Hddqjcb.class).equalTo("id",b.id).findAll();
                Hddqjcb alter = br.get(0);
                alter.jcr = b.jcr;
                alter.zgfzr = b.zgfzr;
            }
        });
    }
    public static List<Hddqjcb> getCulvertDQList(String hdcode) {
        List<Handong> mList = realm.where(Handong.class).findAll();
        for (int i = 0; i < mList.size(); i++) {
            if (hdcode.equals(mList.get(i).hdcode)) {
                return mList.get(i).dqjc;
            }
        }
        return null;
    }
    public static List<Hdjcxjcb> getCulvertJCList(String hdcode) {
        List<Handong> mList = realm.where(Handong.class).findAll();
        for (int i = 0; i < mList.size(); i++) {
            if (hdcode.equals(mList.get(i).hdcode)) {
                return mList.get(i).jcxjc;
            }
        }
        return null;
    }

    /**
     * 获取一个经常检查对象的缺损列表
     *
     */
    public static List<HdjcxjcShqkb> getHdjcxjcShqkbList(final String qlcode, final String qldqjcid) {
        Hdjcxjcb bdq = getCulvertJC(qlcode,qldqjcid);

        return bdq.bjshpk;
    }
    /**
     * 获取一个经常检查对象的缺损列表
     *
     */
    public static List<HddqjcBjshb> getHddqjcBjshb(final String qlcode, final String qldqjcid) {
        Hddqjcb bdq = getCulvertDQ(qlcode,qldqjcid);

        return bdq.hdbjqk;
    }
    /**
     * 删除经常检查缺损列表一个对象
     * @param id
     */
    public static void delHdjcxjcShqkb(final String id) {
        realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                RealmResults<HdjcxjcShqkb> br = realm.where(HdjcxjcShqkb.class).equalTo("id",id).findAll();
                br.deleteAllFromRealm();
            }
        });
    }
    /**
     *
     * @param id
     */
    public static HdjcxjcShqkb getCulvertJCbjqs(final String id) {
        final HdjcxjcShqkb[] obj = new HdjcxjcShqkb[1];
        realm.executeTransaction(new Realm.Transaction() {


            @Override
            public void execute(Realm realm) {
                RealmResults<HdjcxjcShqkb> br = realm.where(HdjcxjcShqkb.class).equalTo("id",id).findAll();
                obj[0] = br.get(0);

            }

        });
        return obj[0];

    }

//    /**
//     *
//     * @param id
//     */
//    public static HddqjcBjshb getHddqjcBjshb(final String id) {
//        final HddqjcBjshb[] obj = new HddqjcBjshb[1];
//        realm.executeTransaction(new Realm.Transaction() {
//
//
//            @Override
//            public void execute(Realm realm) {
//                RealmResults<HddqjcBjshb> br = realm.where(HddqjcBjshb.class).equalTo("id",id).findAll();
//                obj[0] = br.get(0);
//
//            }
//
//        });
//        return obj[0];
//
//    }
    /**
     * 删除经常检查缺损列表一个对象
     * @param id
     */
    public static void delHddqjcBjshb(final String id) {
        realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                RealmResults<HddqjcBjshb> br = realm.where(HddqjcBjshb.class).equalTo("id",id).findAll();
                br.deleteAllFromRealm();
            }
        });
    }
    /**
     *
     * @param id
     */
    public static HddqjcBjshb getHddqjcBjshb(final String id) {
        final HddqjcBjshb[] obj = new HddqjcBjshb[1];
        realm.executeTransaction(new Realm.Transaction() {


            @Override
            public void execute(Realm realm) {
                RealmResults<HddqjcBjshb> br = realm.where(HddqjcBjshb.class).equalTo("id",id).findAll();
                obj[0] = br.get(0);

            }

        });
        return obj[0];

    }
    public static void insertHD(Handong hd) {
        PatrolApp.realm.beginTransaction();
        PatrolApp.realm.insert(hd);
        PatrolApp.realm.commitTransaction();


    }

    /**
     * 删除一个涵洞对象
     *
     * @param hd
     */
    public static void delHD(final Handong hd) {
        realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                //一个涵洞对象
                RealmResults<Handong> hds = realm.where(Handong.class).equalTo("hdcode",hd.hdcode).findAll();
                //一个涵洞定期列表
                RealmResults<Hddqjcb> dqs = realm.where(Hddqjcb.class).equalTo("hdcode",hd.hdcode).findAll();
                //一个涵洞经常列表
                RealmResults<Hdjcxjcb> jcs = realm.where(Hdjcxjcb.class).equalTo("hdcode",hd.hdcode).findAll();
//

                if (dqs.size()>0){
                    for (int i=0;i<dqs.size();i++){
                        //一个涵洞定期缺损列表
                        RealmResults<HddqjcBjshb> qss = realm.where(HddqjcBjshb.class).equalTo("hddqjcid",dqs.get(i).id).findAll();
                        qss.deleteAllFromRealm();
                    }
                }


                if (jcs.size()>0){
                    for (int i=0;i<jcs.size();i++){
                        RealmResults<HdjcxjcShqkb> jxqss = realm.where(HdjcxjcShqkb.class).equalTo("hdjcxjcid",jcs.get(i).id).findAll();
                        jxqss.deleteAllFromRealm();
                    }
                }

                dqs.deleteAllFromRealm();
                jcs.deleteAllFromRealm();
                hds.deleteAllFromRealm();


            }
        });


    }
    public static void clearHList() {
        PatrolApp.realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                realm.delete(Handong.class);
                realm.delete(Hddqjcb.class);
                realm.delete(Hdjcxjcb.class);
                realm.delete(HddqjcBjshb.class);
                realm.delete(HdjcxjcShqkb.class);
            }
        });
    }

    /**
     * 修改定期检查list为上传状态
     */
    public static void setHDDQListUpState(final String hdcode, final boolean b) {
        PatrolApp.realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                //先查找后得到对象
                List<Hddqjcb> noups = PatrolApp.realm.where(Hddqjcb.class).equalTo("hdcode",hdcode)
                        .equalTo("isUp",!b).findAll();
                for (int i=0;i<noups.size();i++){
                    noups.get(i).isUp = b;
                }
            }
        });
    }
    /**
     * 修改定期检查对象上传状态
     */
    public static void setHDDQUpState(final String hddqjcid, final boolean b) {
        PatrolApp.realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                //先查找后得到对象
                List<Hddqjcb> noups = PatrolApp.realm.where(Hddqjcb.class).equalTo("id",hddqjcid)
                        .equalTo("isUp",!b).findAll();
                for (int i=0;i<noups.size();i++){
                    noups.get(i).isUp = b;
                }
            }
        });
    }
    /**
     * 初始化定期检查list为上传状态
     */
    public static void setHDDQListUpState(final List<Hddqjcb> mList, final boolean b) {
        PatrolApp.realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                for (int i=0;i<mList.size();i++){
                    mList.get(i).isUp = b;
                }
            }
        });
    }
    /**
     * 修改经常检查没有上传list为上传状态
     */
    public static void setHDJCListUpState(final String hdcode, final boolean b) {
        PatrolApp.realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                //先查找后得到对象
                List<Hdjcxjcb> noups = PatrolApp.realm.where(Hdjcxjcb.class).equalTo("hdcode",hdcode)
                        .equalTo("isUp",!b).findAll();
                for (int i=0;i<noups.size();i++){
                    noups.get(i).isUp = b;
                }
            }
        });
    }

    /**
     * 修改经常检查没有上传对象上传状态
     */
    public static void setHDJCUpState(final String hdjcxjcid, final boolean b) {
        PatrolApp.realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                //先查找后得到对象
                List<Hdjcxjcb> noups = PatrolApp.realm.where(Hdjcxjcb.class).equalTo("id",hdjcxjcid)
                        .equalTo("isUp",!b).findAll();
                for (int i=0;i<noups.size();i++){
                    noups.get(i).isUp = b;
                }
            }
        });
    }
    /**
     * 初始化经常检查没有上传list为上传状态
     */
    public static void setHDJCListUpState(final List<Hdjcxjcb> mList, final boolean b) {
        PatrolApp.realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                for (int i=0;i<mList.size();i++){
                    mList.get(i).isUp = b;
                }
            }
        });
    }
    /**
     * 获取定期检查没有上传的list
     *
     * @param hdcode
     * @return
     */
    public static List<Hddqjcb> getHDDQListNoUp(String hdcode) {
        List<Hddqjcb> noups = PatrolApp.realm.where(Hddqjcb.class).equalTo("hdcode",hdcode)
                .equalTo("isUp",false).findAll();
        return  noups;
    }
    /**
     * 获取经常检查没有上传list
     *
     * @param hdcode
     * @return
     */
    public static List<Hdjcxjcb> getHDJCListNoUp(String hdcode) {
        List<Hdjcxjcb> noups = PatrolApp.realm.where(Hdjcxjcb.class).equalTo("hdcode",hdcode)
                .equalTo("isUp",false).findAll();
        return noups;
    }

    /**
     * 修改桥梁对象为上传状态
     *
     * @param hdcode
     * @return
     */
    public static void setHDUpState(final String hdcode, final boolean b) {
        PatrolApp.realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                //先查找后得到对象
                List<Handong> qls = PatrolApp.realm.where(Handong.class).equalTo("hdcode",hdcode)
                        .findAll();
                for (int i=0;i<qls.size();i++){
                    qls.get(i).isUp = b;
                }
            }
        });
    }

    /**
     * 初始化涵洞对象为上传状态
     */
    public static void setHDUpState(final List<Handong> hds, final boolean b) {
        PatrolApp.realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                for (int i=0;i<hds.size();i++){
                    hds.get(i).isUp = b;
                }
            }
        });
    }

    /**
     * 改变勾选状态
     */
    public static void setcbState(final String hdcode, final boolean b) {
        PatrolApp.realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                RealmResults<Handong> hds = realm.where(Handong.class).equalTo("hdcode",hdcode).findAll();
                hds.get(0).isCheck = b;
            }
        });
    }
    /**
     * 改变勾选状态
     */
    public static void setcbState(final List<Handong> mList, final boolean b) {
        PatrolApp.realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                for (int i=0;i<mList.size();i++){
                    mList.get(i).isCheck = b;
                }
            }
        });
    }
    /**
     * 添加序号
     * 1 定期
     * 2 定期损坏
     * 3 经常
     * 4 经常损坏
     */
     public static List<Hddqjcb> setDQIndex(final List<Hddqjcb> list) {
        PatrolApp.realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                    for (int i=0;i<list.size();i++){
                        list.get(i).index = i+1;
                    }

            }
        });
         return list;
    }
    public static List<Hdjcxjcb>  setJCIndex(final List<Hdjcxjcb> list) {
        PatrolApp.realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                    for (int i=0;i<list.size();i++){
                        list.get(i).index = i+1;
                    }

            }
        });
        return list;
    }
    public static List<HddqjcBjshb>  setDQQSIndex(final List<HddqjcBjshb> list) {
        PatrolApp.realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                for (int i=0;i<list.size();i++){
                    list.get(i).index = i+1;
                }

            }
        });
        return list;
    }
    public static List<HdjcxjcShqkb>  setJCQSIndex(final List<HdjcxjcShqkb> list) {
        PatrolApp.realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                for (int i=0;i<list.size();i++){
                    list.get(i).index = i+1;
                }

            }
        });
        return list;
    }
}
